<template>
    <Dropdown trigger="click" @on-click="changeMenu" >
        <a href="javascript:void(0)">
            {{menuText}}
            <Icon type="ios-arrow-down"></Icon>
        </a>
        <DropdownMenu slot="list" style="width:150px;line-height:30px;" >
            <Dropdown placement="right-start" v-for="item in nodeGroupList" :key="item.nodeCode" :value="item.nodeCode" style="height:10px;">
                <DropdownItem :name="item.nodeCode+'/%/'+item.nodeName">
                    {{item.nodeName}}
                    <Icon type="ios-arrow-forward" v-if="item.workGroupList.length>0"></Icon>
                </DropdownItem>
                <DropdownMenu slot="list" v-for="subItem in item.workGroupList" :key="subItem.groupCode" :value="subItem.groupCode" v-if="item.workGroupList.length>0">
                    <DropdownItem  :name="item.nodeCode+'/%/'+subItem.groupCode+'/%/'+item.nodeName+'-'+subItem.groupName">{{subItem.groupName}}</DropdownItem>
                </DropdownMenu>
            </Dropdown>
        </DropdownMenu>
    </Dropdown>
</template>
<script>
export default {
  name: 'Workgroup',
  data () {
    return {
      nodeGroupList: null,
      menuText: '选择节点',
      nodeCode: null,
      groupCode: null,
      $ajax: null
    }
  },
  methods: {
    changeMenu (name) {
      if (!name || name === '' || name == null) {
        this.menuText = '选择节点'
      } else {
        var strArray = name.split('/%/')
        if (strArray.length === 2) {
          this.nodeCode = strArray[0]
          this.menuText = strArray[1]
          sessionStorage['nodeCode'] = this.nodeCode
          sessionStorage['menuText'] = this.menuText
        } else if (strArray.length === 3) {
          this.nodeCode = strArray[0]
          this.groupCode = strArray[1]
          this.menuText = strArray[2]
          sessionStorage['nodeCode'] = this.nodeCode
          sessionStorage['groupCode'] = this.groupCode
          sessionStorage['menuText'] = this.menuText
        } else {
          this.menuText = name
        }
        var index = this.menuText.indexOf('-')
        var nodeName = null
        var groupName = null
        if (index > -1) {
          nodeName = this.menuText.substring(0, index)
          groupName = this.menuText.substring(index + 1, this.menuText.length)
        }
        var param = {
          nodeCode: this.nodeCode,
          groupCode: this.groupCode,
          userCode: sessionStorage.userCode,
          nodeName: nodeName,
          groupName: groupName
        }
        if (this.nodeCode != null && param.userCode !== '' && param.userCode != null) {
          this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/workgroup/service/insertSwitchLog', param).then(re => {
            var datas = re.data.data || []
            var userList = []
            datas.forEach(item => {
              var tData = {}
              tData.label = item.userName
              tData.value = item.userCode
              userList.push(tData)
            })
            vm.users = userList
          }).catch(er => {
          })
        }
      }
    }

  },
  created: function () {
    this.$ajax = this.$util.ajax()
    this.nodeGroupList = sessionStorage.nodeGroupList === '' ? null : JSON.parse(sessionStorage.nodeGroupList)
    if (sessionStorage.menuText) {
      this.menuText = sessionStorage.menuText
    }
  }
}
</script>
